home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / msgq160s.arc / AREAS.C next >
Text File  |  1991-10-26  |  10KB  |  397 lines

  1. /*
  2.  * AREAS.C - Message area display
  3.  *
  4.  * Msged/Q message editor for QuickBBS  Copyright 1990 by P.J. Muller
  5.  *
  6.  * Message area adapted from Johan Zwiekhorst's modifications to Msged
  7.  *
  8.  */
  9.  
  10. /* #define SPEEDUP */            /* (not significant) */
  11.  
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15.  
  16. #include "msged.h"
  17. #include "screen.h"
  18. #include "qmsgbase.h"
  19.  
  20. static BOOLEAN showdesc = TRUE;        /* tag/description flag */
  21.  
  22. #define MENUWIDTH 23
  23.  
  24. static int aROWS, aCOLS, aMAX;    /* per page */
  25.  
  26. #define INDIC '\20'        /* Triangle pointing to the right */
  27.  
  28. #define NormalVideo  0
  29. #define ReverseVideo 1
  30.  
  31. #ifdef SPEEDUP
  32. static int *keeplastmsg=NULL;    /* array for speedup */
  33. static void initspeedup(void);
  34. static void donespeedup(void);
  35. static int LASTMSG(BYTE b);
  36. #else                /* disable everything */
  37. #  define LASTMSG(b) lastmsg(b)
  38. #  define initspeedup();
  39. #  define donespeedup();
  40. #endif
  41.  
  42. void display_area(int anum, int textcolor);
  43. void display_page(int pnum, int pmax);
  44. static void dop(char *title);
  45.  
  46. #ifdef SPEEDUP
  47. /*
  48.  * Initialise mechanism for speeding up Alt-T
  49.  */
  50.  
  51. static void initspeedup(void)
  52. {
  53.   int c;
  54.  
  55.   if (keeplastmsg == NULL)
  56.     if ((keeplastmsg = malloc(sizeof(int)*BLIM)) == NULL)
  57.       return;
  58.  
  59.   for (c=0;  c < BLIM;  c++)
  60.     keeplastmsg[c] = -1;
  61. } /* initspeedup */
  62.  
  63. static void donespeedup(void)
  64. {
  65.   if (keeplastmsg == NULL)
  66.     return;
  67.  
  68.   ptrfree(keeplastmsg);
  69.   keeplastmsg = NULL;
  70. } /* donespeedup */
  71.  
  72. static int LASTMSG(BYTE b)
  73. {
  74.   if (keeplastmsg == NULL) {
  75.     return lastmsg(b);
  76.   } else {
  77.     if (keeplastmsg[b-1] == -1)
  78.       return keeplastmsg[b-1] = lastmsg(b);
  79.     else
  80.       return keeplastmsg[b-1];
  81.   } /* else */
  82. } /* LASTMSG */
  83. #endif    /* SPEEDUP */
  84.  
  85. /*
  86.  * Draw the surroundings
  87.  */
  88.  
  89. static void dop(char *title)
  90. {
  91.     int x,y;
  92.  
  93.     set_color(co_normal);
  94.     cls();
  95.  
  96.     /* Draw frame */
  97.  
  98. #ifdef NOIBM
  99.     for (x = 2; x < maxx; x++) {
  100.       gotoxy(x, 1); bputc('-');
  101.       gotoxy(x, 6); bputc('-');
  102.       gotoxy(x,maxy+1); bputc('-');
  103.     }
  104.     for (y = 2; y < maxy; y++) {
  105.         gotoxy(   1,y); bputc('|');
  106.         gotoxy(maxx,y); bputc('|');
  107.         }
  108.     gotoxy( 1, 1);   bputc('/');   gotoxy(maxx, 1);   bputc('\\');
  109.     gotoxy( 1,maxy); bputc('\\');   gotoxy(maxx,maxy); bputc('/');
  110. #else
  111.     for (x = 2; x < maxx; x++) {
  112.             gotoxy(x, 1); bputc('─');
  113.         gotoxy(x, 6); bputc('─');
  114.         gotoxy(x,maxy+1); bputc('─');
  115.     }
  116.     for (y = 2; y < maxy; y++) {
  117.         gotoxy(   1,y); bputc('│');
  118.         gotoxy(maxx,y); bputc('│');
  119.         }
  120.     gotoxy( 1, 1);   bputc('┌');   gotoxy(maxx, 1);   bputc('┐');
  121.     gotoxy( 1, 6);   bputc('├');   gotoxy(maxx, 6);   bputc('┤');
  122.     gotoxy( 1,maxy); bputc('└');   gotoxy(maxx,maxy); bputc('┘');
  123. #endif
  124.  
  125.     gotoxy((maxx-57)/2, 2);
  126.         bputs("Position cursor on desired area with the arrow keys, then");
  127.     gotoxy((maxx-57)/2, 3);
  128.     bputs("press <Enter> to select.  <Alt-H> for Help Page.");
  129.  
  130.     set_color(co_info);
  131.     if (title == NULL)
  132.       title = "SELECT A MESSAGE AREA";
  133.     gotoxy((maxx-strlen(title))/2, 1);
  134.     bprintf(" %s ", title);
  135.     set_color(co_normal);
  136.  
  137.     gotoxy(1,maxy);
  138.     video_update();
  139. } /* dop */
  140.  
  141. /* selectarea() returns -1 when aborted */
  142.  
  143. int     selectarea(char *title)
  144. {
  145.     int     y, ch=0, a=area, p=0, b;
  146.         int     pagenum=1, pagemax;
  147.         char    maybe[MENUWIDTH+1];
  148.         char    *adesc;
  149.  
  150.     if (areas <= 0) return -1;
  151.  
  152.         memset(maybe, 0, sizeof(maybe));
  153.  
  154.     aROWS = maxy-9;
  155.     aCOLS = (maxx-4)/(MENUWIDTH+2);
  156.     aMAX = (aROWS*aCOLS);
  157.     dop(title);
  158.  
  159.     initspeedup();
  160.  
  161.         /* Display area descriptions */
  162.  
  163.         pagenum = a/aMAX + 1;
  164.     pagemax = (areas-1)/aMAX + 1;
  165.         display_page(pagenum, pagemax);
  166.  
  167.         /* get keys until ENTER or ESC pressed */
  168.  
  169.         while ((ch != ENTER) && (ch != ABORT)) {
  170.               display_area(a, ReverseVideo);
  171.  
  172.           gotoxy((maxx-52), 5);
  173.           set_color(co_info);
  174.           y = countmsg(arealist[a].board);
  175.           if (y == 0)
  176.          bputs("  no");
  177.           else
  178.          bprintf("%4d",y);
  179.           y -= boardmsg(arealist[a].board,lastreadmsg(arealist[a].board));
  180.           bputs(" messages in current area,");
  181.           if (y == 0)
  182.         bputs(" none unread.  ");
  183.           else
  184.         bprintf("%5d unread.  ",y);
  185.           set_color(co_normal);
  186.  
  187.           ch = getkey();
  188.  
  189.               display_area(a, NormalVideo);
  190.  
  191.               switch (ch) {
  192.  
  193.           case TOGGLE:            /* toggle */
  194.         showdesc = !showdesc;
  195.         memset(maybe, 0, sizeof(maybe));
  196.         p = 0;
  197.         display_page(pagenum, pagemax);
  198.         break;
  199.  
  200.               case LEFT:
  201.                         p=0; memset(maybe, 0, sizeof(maybe));
  202.                         if ((a % aMAX) >= aROWS)
  203.                            a -= aROWS;
  204.                         break;
  205.  
  206.               case RIGHT:
  207.                          p=0; memset(maybe, 0, sizeof(maybe));
  208.                          if ((a + aROWS) < min(areas,aMAX*pagenum))
  209.                             a += aROWS;
  210.                          break;
  211.  
  212.               case UP:
  213.                          p=0; memset(maybe, 0, sizeof(maybe));
  214.                          if (a > 0) {
  215.                            a--;
  216.                            if ((a+1) == (aMAX*(pagenum-1))) {
  217.                               pagenum--;
  218.                               display_page(pagenum,pagemax);
  219.                            }
  220.                          }
  221.                          break;
  222.  
  223.               case DOWN:
  224.                         p=0; memset(maybe, 0, sizeof(maybe));
  225.                         if (a < (areas-1)) {
  226.                            a++;
  227.                            if (a == (aMAX*pagenum)) {
  228.                               pagenum++;
  229.                               display_page(pagenum,pagemax);
  230.                            }
  231.                         }
  232.                         break;
  233.  
  234.           case '+': {
  235.         int look = a;    /* look for next board with mail */
  236.         while ((++look >= areas ? look = 0 : look) != a)
  237.           if (lastreadmsg(arealist[look].board) < LASTMSG(arealist[look].board))
  238.             break;
  239.         if (look != a) {
  240.           p=0; memset(maybe, 0, sizeof(maybe));
  241.           a = look;
  242.         }
  243.         b = a/aMAX + 1;
  244.         if (pagenum != b) {
  245.            pagenum = b;
  246.            display_page(pagenum,pagemax);
  247.         }
  248.                 break;
  249.           }
  250.  
  251.           case '-': {
  252.         int look = a;    /* look for next board with mail */
  253.         while ((--look < 0 ? look = areas-1 : look) != a)
  254.           if (lastreadmsg(arealist[look].board) < LASTMSG(arealist[look].board))
  255.             break;
  256.         if (look != a) {
  257.           p=0; memset(maybe, 0, sizeof(maybe));
  258.           a = look;
  259.         }
  260.         b = a/aMAX + 1;
  261.         if (pagenum != b) {
  262.            pagenum = b;
  263.            display_page(pagenum,pagemax);
  264.         }
  265.         break;
  266.           }
  267.  
  268.           case GOBOL:
  269.             p=0; memset(maybe, 0, sizeof(maybe));
  270.             /* a = aMAX * (pagenum - 1); */
  271.             a = 0;
  272.             if (pagenum != 1) {
  273.               pagenum = 1;
  274.               display_page(pagenum,pagemax);
  275.             }
  276.             break;
  277.  
  278.           case GOEOL:
  279.             p=0; memset(maybe, 0, sizeof(maybe));
  280.             /* a = aMAX * pagenum - 1;
  281.             if (a >= areas) */
  282.             a = areas-1;
  283.             if (pagenum != pagemax) {
  284.               pagenum = pagemax;
  285.               display_page(pagenum,pagemax);
  286.             }
  287.             break;
  288.  
  289.               case PGUP:
  290.             p=0; memset(maybe, 0, sizeof(maybe));
  291.             if (a % aROWS != 0) {
  292.                a -= a % aROWS;
  293.             } else if (pagenum > 1) {
  294.                            pagenum--;
  295.                            a = aMAX * (pagenum - 1);
  296.                            display_page(pagenum, pagemax);
  297.                         }
  298.                         break;
  299.  
  300.               case PGDN:
  301.             p=0; memset(maybe, 0, sizeof(maybe));
  302.             if (a % aROWS != aROWS-1) {
  303.                a += aROWS - (a % aROWS) - 1;
  304.                if (a >= areas)
  305.                  a = areas-1;
  306.             } else if (pagenum < pagemax) {
  307.                            pagenum++;
  308.                            a = aMAX * (pagenum - 1);
  309.                            display_page(pagenum, pagemax);
  310.                         }
  311.                         break;
  312.  
  313.           case ENTER:
  314.             donespeedup();
  315.             return(a);
  316.  
  317.           case ABORT:
  318.             donespeedup();
  319.             return(-1);
  320.  
  321.           case HELP:
  322.             helparea();
  323.             dop(title);
  324.             display_page(pagenum, pagemax);
  325.             break;
  326.  
  327.           case DONE2:
  328.             cleanup();
  329.             break;
  330.  
  331.           default:
  332.               if ((ch & 0xff) == 0) break;
  333.               maybe[p++] = (unsigned char)(ch & 0xff);
  334.                       for (b = 0; b < areas; b++) {
  335.                           adesc = strlwr(strdup(arealist[b].description));
  336.               if (strncmp(strlwr(maybe), adesc, strlen(maybe)) == 0)
  337.                              a = b;
  338.                           ptrfree(adesc);
  339.                       }
  340.                       b = a/aMAX + 1;
  341.                       if (pagenum != b) {
  342.                          pagenum = b;
  343.                          display_page(pagenum,pagemax);
  344.                       }
  345.               }
  346.     } /* while */
  347.     donespeedup();
  348.     return(area);
  349. } /* selectarea */
  350.  
  351. void    display_page(int pnum, int pmax)
  352. {
  353.  int    anum, count;
  354.  
  355.  gotoxy( 3, 5);
  356.  set_color(co_quote);
  357.  bprintf("Page %d of %d",pnum,pmax);
  358.  set_color(co_normal);
  359.  clrwnd(3,8,maxx-2,maxy-2);
  360.  anum = aMAX * (pnum - 1);
  361.  count = 1;
  362.  while ((anum < areas) && (count++ <= aMAX))
  363.        display_area(anum++, NormalVideo);
  364. } /* display_page */
  365.  
  366. void    display_area(int anum, int reversecolor)
  367. {
  368.   int   a, x, y;
  369.   char  desc[MENUWIDTH+1];
  370.  
  371.   a = anum % aMAX;
  372.   x = (MENUWIDTH+2) * (a / aROWS) + 3;
  373.   y = 8 + (a % aROWS);
  374.   gotoxy(x+1, y);
  375.  
  376.   set_color(co_info);
  377.   if (lastreadmsg(arealist[anum].board) < LASTMSG(arealist[anum].board))
  378.     bputc(INDIC);
  379.   else
  380.     bputc(' ');
  381.  
  382.   if (reversecolor)
  383.      set_color(co_hilite);
  384.   else
  385.      set_color(co_normal);
  386.   if (!showdesc) {
  387.     char buf[80];
  388.     sprintf(buf,"%3d %1.70s", arealist[anum].board, arealist[anum].tag);
  389.     strncpy(desc, buf, MENUWIDTH);
  390.   } else {
  391.     strncpy(desc, arealist[anum].description, MENUWIDTH);
  392.   } /* else */
  393.   desc[MENUWIDTH] = '\0';
  394.   bputs(desc);
  395.   set_color(co_normal);
  396. } /* display_area */
  397.